package Framework;
/*
 * 
 * 	nv_ImageLoader
 * 	If image already exists in memory, return a reference to that, otherwise, load it, store
 * 	a reference, and return the reference.  	
 * 
 * 
 * 	Notes
 *  -----------------------------------------------------------------------------------------
 *  I still havn't figured out how to create static classes, or global methods. :(
 *   
 *  To Do
 * 	-----------------------------------------------------------------------------------------
 * 	Probably about complete.  
 * 	
 *  Revision History
 *  ------------------------------------------------------------------------------------------
 *  Who					Date			What's changed & Why & Notes
 * 	Colin Smith			9/10/2011		Created. Pulled from common code between anc_*   
 * 
 */

import java.awt.Image;
import java.io.File;
import java.util.Vector;

import javax.swing.ImageIcon;




public class nv_ImageLoader {
	static String CLASS = "nv_ImageLoader";
	
	private static Vector<ImageIcon> d_img_ImageList = new Vector<ImageIcon>(); // Instead of loading the same image multiple times
	private static Vector<String> d_img_FileList = new Vector<String>();// Instead of loading the same image multiple times
	private static String is_themeName = "baseTheme";
	
	public static void changeTheme(String newThemeName){
		if (is_themeName != newThemeName){
			is_themeName = newThemeName;
			
			Vector <String> str_FileList = d_img_FileList;
			//d_img_FileList = new Vector<String>();
			//d_img_ImageList = new Vector<ImageIcon>();
			//System.out.println(str_FileList.size());
			//System.exit(1);
			for (int i=0; i < d_img_FileList.size(); i++){
				//d_img_ImageList.elementAt(i).setImage(new Image());// = LoadImageFromFile(d_img_FileList.elementAt(i));
				//d_img_ImageList.elementAt(i).setImage();
				loadImage(str_FileList.elementAt(i));
			}
			
		}
	}
	private static ImageIcon LoadImageFromFile(String as_image){
		String METHOD = "loadImageFromFile(String " + as_image + ")";
		debugMessage.log(CLASS + "." + METHOD + " Called.", 10);
		
		String as_imageT = "img/" + is_themeName + "/" + as_image;
		
		// Check exists. If not return stub image.
		if (! new File(as_imageT).exists()){
			debugMessage.log(CLASS + "." + METHOD + " Image '" + as_imageT + "' Does not exist. Using a stub", 90);
			return loadImage("img/404_fffuuu.png");
		}
		
		// Did not find, loading from disk.
		return new ImageIcon(as_imageT);
	}
	
	static public ImageIcon loadImage(String as_image){
		String METHOD = "loadImage(String " + as_image + ")";
		debugMessage.log(CLASS + "." + METHOD + " Called.", 10);
		
		
		// Iterate vectors, if filename is in it, return that. 
		for ( int i = 0; i < d_img_ImageList.size(); i++){
			if (d_img_FileList.elementAt(i) == as_image){
					debugMessage.log(CLASS + "." + METHOD + " Finished. Image already in memory.", 90);
					return d_img_ImageList.elementAt(i);
			}
		}
		
		
		ImageIcon myImgIcon =LoadImageFromFile(as_image);
		d_img_ImageList.add(myImgIcon);
		d_img_FileList.add(as_image);
		debugMessage.log(CLASS + "." + METHOD + " Finished. Image loaded from file", 10);
		return myImgIcon;
	}
}
